# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
from dataclasses import dataclass
from ..user_logbook import user_log as logger


@dataclass
class CFutureKlineItem:
    """
    一个品种的单条数据
    """
    open: float = 0.0  # 开盘价
    high: float = 0.0  # 最高价
    low: float = 0.0  # 最低价
    close: float = 0.0  # 收盘价
    volume: int = 0  # 成交量
    amount: float = 0.0  # 成交额
    open_oi: float = 0.0  # K线开始时的持仓量
    close_oi: float = 0.0  # K线结束时的持仓量
    date_time: datetime = datetime.datetime.fromtimestamp(0)  # 数据时间
    update_time: datetime = datetime.datetime.fromtimestamp(0)  # 数据更新时间，精确到毫秒


class CFutureKlineStore:
    """周期数据的存储"""
    @staticmethod
    def replace_into_future_history_table(mysql, table_name, items, symbol):
        for i in items:
            sql = "insert into {}(code,datetime," \
                  "open,high,low,close,volume,open_oi,close_oi)VALUES" \
                  "('{}','{}',{},{},{},{},{},{},{})".format(table_name, symbol, str(i.date_time), i.open, i.high, i.low,
                                                            i.close, i.volume, i.open_oi, i.close_oi)
            logger.debug(sql)
            mysql.exec(sql)
